package xFGE.net
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.getTimer;
	import flash.system.System;
	
	public class HttpConect
	{
		static private var _myInstance:HttpConect = null;
		 
		private var _loader:URLLoader;
		
		private var _request:URLRequest;
		
		private var _url:String;
		
		private var _parseFunc:Function;
		
		private var _isBusy:Boolean= false;
		
		private var _requestBuffer:Array;
		
		private var _hasInit:Boolean = false;
		
		public function HttpConect()
		{
			if(_myInstance) throw("请用getInstance获取实例");
		}
		
		public function InitNet(url:String,parseFunc:Function,dataFormat:String = "text"):void
		{
			if(_hasInit) return;
			flash.system.System.useCodePage = true;
			_loader = new URLLoader();
			_request = new URLRequest(url);
			
			_url = url;
			if(parseFunc == null) throw("分析回调函数不能为空");
			_parseFunc = parseFunc;
			_requestBuffer = new Array();
			// 设置loader
			_loader.dataFormat = dataFormat;
			_loader.addEventListener(Event.COMPLETE,_onComplete);
			_loader.addEventListener(IOErrorEvent.IO_ERROR,_onIOError);
			_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,_onHttpStatus);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,_onSecurityError);
			_hasInit = true;
		}
		
		static public function getInstance():HttpConect
		{
			if(_myInstance == null) _myInstance = new HttpConect()
			return _myInstance;
		}
		
		/**
		 * 发送数据 
		 * @param data
		 * @param method
		 * 
		 */
		public function Send(data:Object,method:String = "GET"):void
		{
			// 当辅助器忙时，将当前请求放入缓冲区
			if(_isBusy){
				var requestObj:Object = new Object();
				requestObj.data = data;
				requestObj.method = method;
				_requestBuffer.push(requestObj);
				return;
			}
			
			_request.method = method;
			_request.data = (data as String) + "&"+getTimer();
			try{
				_loader.load(_request);
				_isBusy = true;
			}catch(error:Error)
			{
				trace("不能连接URL:"+_request.url + "错误:"+error);
				_isBusy = false;
				ClearRequestBuffer();
			}
		}
		/**
		 * 清空请求缓冲 
		 * 
		 */
		private function ClearRequestBuffer():void
		{
			_requestBuffer.splice(0,_requestBuffer.length);
		}
		/**
		 * 接收信息 
		 * @param eve
		 * 
		 */
		private function _onComplete(eve:Event):void
		{
			trace(_loader.data);
			_isBusy = false;
			_parseFunc(new XML(_loader.data));
			_sendNext();
			
		}
		
		/**
		 * 网络状态监听 
		 * @param eve
		 * 
		 */
		private function _onHttpStatus(eve:HTTPStatusEvent):void
		{
		 	trace("网络状态:"+eve.status);
		}
		
		/**
		 * 安全沙箱错误监听 
		 * @param eve
		 * 
		 */
		private function _onSecurityError(eve:SecurityErrorEvent):void
		{
			trace("安全沙箱错误");
		}
		
		/**
		 * 输入输出错误监听 
		 * @param eve
		 * 
		 */
		private function _onIOError(eve:IOErrorEvent):void
		{
			trace("IO出错");
		}
		/**
		 * 读取缓冲下一个请求 
		 * 
		 */
		private function _sendNext():void
		{
			if(_requestBuffer.length == 0) return;
			var requestObj:Object = _requestBuffer.shift();
			Send(requestObj.data,requestObj.method);
		}

	}
}